/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bgu.sonar.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author bennyl
 */
public abstract class ListMap<K, V> extends HashMap<K, List<V>> {

    public void add(K key, V value) {
        List<V> l = get(key);
        if (l == null) {
            l = newList();
            put(key, l);
        }

        l.add(value);
    }

    protected abstract List<V> newList();

    public static class LinkedListMap<K, V> extends ListMap<K, V> {

        @Override
        protected List<V> newList() {
            return new LinkedList<>();
        }
    }

    public static class ArrayListMap<K, V> extends ListMap<K, V> {

        @Override
        protected List<V> newList() {
            return new ArrayList<>();
        }
    }

}
